var mongoose = require('../util/db');

var ArticleSchema = mongoose.Schema({
    title: String,
    content: String,
    active: { type: Boolean, default: true }
});

var Article = mongoose.model('Article', ArticleSchema);

exports.save = function (title, content, fn) {
    var article = new Article({
        title: title,
        content: content
    });

    article.save(function (err,data) {
        fn(err,data);
    });
}

exports.get = function (id, fn) {
    Article.findById(id, function (err, article) {
        fn(err, article);
    });
}

exports.list = function (fn) {
    Article.find({ active: true }, function (err, articles) {
        fn(err, articles);
    });
}

exports.listByIds=function(ids,fn){
    Article.find({
        active:true,
        _id:{
            $in: ids
        }
    },function(err,acticles){
        fn(err,acticles);
    })
}

exports.update = function (id, title, content, fn) {
    Article.update({
        _id: id
    }, {
            $set: {
                title: title,
                content: content
            }
        }, function (err) {
            fn(err);
        });
}

exports.delete = function (id, fn) {
    Article.update({
        _id: id
    }, {
            $set: {
                active: false
            }
        }, function (err) {
            fn(err);
        });
}